package com.glory.brepda.activity.mfg;

import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.github.chrisbanes.photoview.PhotoView;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.DrawingpathEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;

public class PlasticCheckTimeActivity extends BaseActivity {

    private long timeLeftInMillis;
    private int cdbgcolorid;
    private CountDownTimer countDownTimer;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.barcodeinfo)
    FreeUI_ClearEditText barcodeinfo;
    @BindView(R.id.countdown)
    TextView countdowm;
    @BindView(R.id.cdbackground)
    LinearLayout cdbackground;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_plasticchecktime;
    }

    @Override
    protected void initView() {

        setMainLayout(llMain);
        initEdit(barcodeinfo, s -> {
            if(barcodeinfo.getText().toString().length()>20) {
                if (countDownTimer != null) {
                    countDownTimer.onFinish();
                }
                cdbackground.setBackgroundColor(-1);
                countdowm.setText("");
                String[] Info = barcodeinfo.getText().toString().split("\\|");
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String current = simpleDateFormat.format(date);
                ColorDrawable colorDrawable = (ColorDrawable) cdbackground.getBackground();
                cdbgcolorid = colorDrawable.getColor();
                try {
                    if (simpleDateFormat.parse(current).getTime() < simpleDateFormat.parse(Info[3]).getTime()) {
                        showConfirmDialog("塑封料醒料未结束");
                    } else if (simpleDateFormat.parse(current).getTime() > simpleDateFormat.parse(Info[4]).getTime()) {
                        cdbackground.setBackgroundColor(getResources().getColor(R.color.red));
                        showConfirmDialog("塑封料醒料有效期已超期");
                    } else {
                        long ms = simpleDateFormat.parse(Info[4]).getTime() - simpleDateFormat.parse(current).getTime();
                        int hour = Integer.parseInt(String.valueOf(ms / (1000 * 60 * 60)));
                        int minutes = Integer.parseInt(String.valueOf((ms % (1000 * 60 * 60) / (1000 * 60))));
                        startCountdown(hour, minutes);
                    }
                } catch (ParseException e) {

                }
                barcodeinfo.setText("");
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void startCountdown(int hour, int minute) {
        timeLeftInMillis = (hour * 60 + minute) * 60 * 1000;
        // 开始倒计时
        countDownTimer = new CountDownTimer(timeLeftInMillis, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                updateCountdownText(timeLeftInMillis);
            }

            @Override
            public void onFinish() {
                if(countDownTimer!=null && !countdowm.getText().equals("00:00:00")) {
                    countDownTimer.cancel();
                }
                else{
                    cdbackground.setBackgroundColor(getResources().getColor(R.color.red));
                    showConfirmDialog("塑封料醒料有效期已超期");
                    countDownTimer.cancel();
                }
            }
        }.start();
    }

    // 更新倒计时文本
    private void updateCountdownText(long timeLeftInMillis) {
        int hours = (int) (timeLeftInMillis / 1000 / 3600);
        int minutes = (int) ((timeLeftInMillis / 1000) % 3600 / 60);
        int seconds = (int) (timeLeftInMillis / 1000 % 60);
        ColorDrawable colorDrawable = (ColorDrawable) cdbackground.getBackground();
        if(hours < 1 && cdbgcolorid == -1){
            cdbackground.setBackgroundColor(getResources().getColor(R.color.red));
            cdbgcolorid = colorDrawable.getColor();
        }else if(hours<1 && cdbgcolorid != -1){
            cdbackground.setBackgroundColor(-1);
            cdbgcolorid=colorDrawable.getColor();
        }
        countdowm.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
    }
}
